package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Processmethod;
import cn.edu.scau.cmi.ema.domain.Unit;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("ProcessmethodController")
public class ProcessmethodController extends CmiEmaController {

	@RequestMapping("/listProcessmethods")
	public ModelAndView listProcessmethods(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = processmethodPaginationService.updatePagination(request, pagination);
		Set<Processmethod> processmethods = processmethodPaginationService.getPaginationProcessmethods(pagination);

		mav.addObject("processmethods", processmethods);
		mav.addObject("entityName", "processmethod");
		mav.addObject("pagination", pagination);
		mav.setViewName("processmethod/listProcessmethods");
		return mav;
	}

	public String listProcessmethods() {
		return "redirect:/listProcessmethods";
	}

	@RequestMapping("/viewProcessmethod")
	public ModelAndView viewProcessmethod(@RequestParam Integer processmethodId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("processmethod", processmethodDAO.findProcessmethodByPrimaryKey(processmethodId));
		mav.setViewName("processmethod/viewProcessmethod");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteProcessmethods")
	public ModelAndView confirmDeleteProcessmethods(
			@RequestParam(value = "deleteProcessmethodsIds", required = true) Set<Integer> deleteProcessmethodsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Processmethod> deleteProcessmethods = processmethodDAO.findProcessmethodsByIds(deleteProcessmethodsIds);
		Set<Processmethod> referencedProcessmethods = processmethodService
				.loadReferenceProcessmethods(deleteProcessmethods);
		boolean hasReferenced;
		if (referencedProcessmethods.size() == 0) {
			mav.addObject("deleteEntitys", deleteProcessmethods);
			hasReferenced = false;
		} else {
			deleteProcessmethods.removeAll(referencedProcessmethods);
			mav.addObject("referencedEntitys", referencedProcessmethods);
			mav.addObject("deleteEntitys", deleteProcessmethods);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Processmethod");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listProcessmethods");
		return mav;
	}

	@RequestMapping("/deleteProcessmethod")
	public String deleteProcessmethod(@RequestParam Integer processmethodId) {
		Processmethod processmethod = processmethodDAO.findProcessmethodByPrimaryKey(processmethodId);
		processmethodService.deleteProcessmethod(processmethod);
		return "forward:/listProcessmethods";
	}

	@RequestMapping("/confirmDeleteProcessmethod")
	public ModelAndView confirmDeleteProcessmethod(@RequestParam Integer processmethodId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("processmethod", processmethodDAO.findProcessmethodByPrimaryKey(processmethodId));

		mav.setViewName("processmethod/deleteProcessmethod");

		return mav;
	}

	@RequestMapping("/deleteProcessmethods")
	public String deleteProcessmethods(
			@RequestParam(value = "deleteProcessmethodsId", required = true) Set<Integer> deleteProcessmethodsId) {
		processmethodService.deleteProcessmethods(deleteProcessmethodsId);
		return "forward:/listProcessmethods";
	}

	@RequestMapping("/editProcessmethod")
	public ModelAndView editProcessmethod(@RequestParam Integer processmethodId) {

		ModelAndView mav = new ModelAndView();

		Processmethod processmethod = processmethodDAO.findProcessmethodByPrimaryKey(processmethodId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("processmethod", processmethod);

		// 把所有的引用processmethod对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(processmethodService.loadProcessmethodObjectReferencedSet(processmethod));

		// 将编辑页面processmethod添加到mav中
		mav.setViewName("processmethod/editProcessmethod");
		return mav;
	}

	@RequestMapping("/newProcessmethod")
	public ModelAndView newProcessmethod() {
		ModelAndView mav = new ModelAndView();
		Processmethod processmethod = new Processmethod();

		mav.addObject("processmethod", processmethod);

		// 把所有的引用processmethod对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(processmethodService.loadProcessmethodObjectReferencedSet(processmethod));

		mav.addObject("newFlag", true);
		mav.setViewName("processmethod/editProcessmethod");

		return mav;
	}

	@RequestMapping("/saveProcessmethod")
	public ModelAndView saveProcessmethod(@ModelAttribute Processmethod processmethod) {

		ModelAndView mav = new ModelAndView();

		Unit unitTemp = processmethod.getUnit();
		Unit unit = unitTemp == null ? null : CmiHibernateProxyUtil.deproxy(unitDAO.findUnitById(unitTemp.getId()));

		Processmethod existingProcessmethod = CmiHibernateProxyUtil.deproxy(processmethodDAO
				.findProcessmethodByPhysicalPrimaryKey(processmethod.getName(), processmethod.getFee(), unit.getId()));

		processmethod.setUnit(unit);
		// 新建记录
		if (processmethod.getId() == null) {
			if (processmethod.equals(existingProcessmethod)) {

				mav.addAllObjects(processmethodService.loadProcessmethodClassReferencedSet(processmethod));
				// mav = editContinue(mav, processmethod);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("processmethod/editProcessmethod");
			} else {
				processmethodService.saveProcessmethod(processmethod);
				mav.setViewName("forward:/listProcessmethods");
			}
		}
		// 编辑记录
		else {
			Processmethod originProcessmethod = CmiHibernateProxyUtil
					.deproxy(processmethodDAO.findProcessmethodByPrimaryKey(processmethod.getId()));
			if (!processmethod.equals(originProcessmethod) && processmethod.equals(existingProcessmethod)) {
				mav.addAllObjects(processmethodService.loadProcessmethodClassReferencedSet(processmethod));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("processmethod/editProcessmethod");
			} else {
				processmethodService.saveProcessmethod(processmethod);
				mav.setViewName("forward:/listProcessmethods");
			}
		}
		return mav;
	}

	@RequestMapping("/processmethodController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
